<?php
require_once(dirname(__FILE__) . '/app.php');

$id = abs(intval($_GET['id']));
if (!$id || !$team = Table::FetchForce('team', $id) ) {
	redirect( WEB_ROOT . '/team/index.php');
}

/* refer */
if ($_rid = abs(intval($_GET['r']))) { 
	if($_rid) cookieset('_rid', abs(intval($_GET['r'])));
	redirect( WEB_ROOT . "/team.php?id={$id}");
}
$city = Table::Fetch('category', $team['city_id']);
if(!$city) { $city = array('id' => 0, 'name' => '全部', ); }

$now = time();

/* progress bar size */
$bar_size = ceil(190*($team['now_number']/$team['min_number']));
$bar_offset = ceil(5*($team['now_number']/$team['min_number']));

$partner = Table::Fetch('partner', $team['partner_id']);

/* other teams */
if ( abs(intval($INI['system']['sideteam'])) ) {
	$oc = array( 			
			"city_id = {$city['id']} OR city_id = 0",			
			"begin_time < {$now}",
			"end_time > {$now}",
			);
	$others = DB::LimitQuery('team', array(
				'condition' => $oc,
				'order' => 'ORDER BY id DESC',
				'size' => abs(intval($INI['system']['sideteam'])+1),
				));
	foreach ($others as &$other){
		//折扣
		$other['discount_price'] = $other['market_price'] - $other['team_price'];
		$other['discount_rate'] = $other['team_price']/$other['market_price']*10;
		//剩余天数
		$left = array();
		$now = time();
		$other['diff_time'] = $diff_time = $left_time = $other['end_time']-$now;
		
		$other['left_day'] = $left_day = floor($diff_time/86400);
		$other['left_time'] = $left_time = $left_time % 86400;
		$other['left_hour'] = $left_hour = floor($left_time/3600);
		$other['left_time'] = $left_time = $left_time % 3600;
		$other['left_minute'] = $left_minute = floor($left_time/60);
		$other['left_time'] = $left_time = $left_time % 60;
		
		$other['state'] = team_state($other);
	}	
	
}

/* your order */
if ($login_user_id && 0==$team['close_time']) {
	$order = DB::LimitQuery('order', array(
		'condition' => array(
			'team_id' => $id,
			'user_id' => $login_user_id,
			'state' => 'unpay',
		),
		'one' => true,
	));
}
/* end order */

include template('teams_view');
